package com.ruoyi.web.cp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author xiesm
 * @Description: TODO
 * @date 2025-09-24 10:23
 */
@RestController
@RequestMapping("/cp")
public class ObController {

    @Data
    public static class ResultData {
        private String msg;
        private Object data;
        private String code;

        public static ResultData success(Object data) {
            ResultData resultData = new ResultData();
            resultData.setCode("0000000");
            resultData.setMsg("成功");
            resultData.setData(data);
            return resultData;
        }
    }

    /**
     * @author xiesm
     * @description ob列表详情
     * @date 2025/9/24
     */
    @RequestMapping("/obList")
    public ResultData obList(@RequestBody(required = false) JSONObject requestBody) {
        String url = requestBody.getString("url");
        JSONArray keys = requestBody.getJSONArray("keys");
        JSONArray datas = requestBody.getJSONArray("datas");

        JSONObject header = requestBody.getJSONObject("header");
        JSONObject body = requestBody.getJSONObject("body");
        JSONObject res = JSONObject.parseObject(HttpUtil.createPost(url)
                .header("requestId", header.getString("requestId"))
                .body(body.toJSONString())
                .execute()
                .body());
//        String str = FileUtil.readString("D:\\乱七八糟\\obList.txt", "utf8");
//        JSONObject res = JSONObject.parseObject(str);

        res = res.getJSONObject("data");
        if (CollUtil.isEmpty(keys)) {
            return ResultData.success(res);
        }

        JSONObject data = new JSONObject();
        JSONArray livedatas = new JSONArray();
        JSONArray nolivedatas = new JSONArray();


        JSONArray subLivedata = res.getJSONArray("livedata");
        JSONArray subNolivedata = res.getJSONArray("nolivedata");

        if (CollUtil.isEmpty(datas) || datas.contains("livedata")) {
            for (int i = 0; i < subLivedata.size(); i++) {
                JSONObject item = subLivedata.getJSONObject(i);
                JSONObject livedata = new JSONObject();
                for (int j = 0; j < keys.size(); j++) {
                    String key = keys.getString(j);
                    livedata.put(key, item.getString(key));
                }
                livedatas.add(livedata);
            }
            data.fluentPut("livedata", livedatas);
        }

        if (CollUtil.isEmpty(datas) || datas.contains("nolivedata")) {
            for (int i = 0; i < subNolivedata.size(); i++) {
                JSONObject item = subNolivedata.getJSONObject(i);
                JSONObject nolivedata = new JSONObject();
                for (int j = 0; j < keys.size(); j++) {
                    String key = keys.getString(j);
                    nolivedata.put(key, item.getString(key));
                }
                nolivedatas.add(nolivedata);
            }
            data.fluentPut("nolivedata", nolivedatas);
        }
        return ResultData.success(data);
    }

    /**
     * @author xiesm
     * @description ob列表详情
     * @date 2025/9/24
     */
    @RequestMapping("/obListDetail")
    public ResultData obListDetail(@RequestBody(required = false) JSONObject requestBody) {
        String url = requestBody.getString("url");
        JSONArray keys = requestBody.getJSONArray("keys");
        JSONObject header = requestBody.getJSONObject("header");
        JSONObject res = JSONObject.parseObject(HttpUtil.createGet(url)
                .header("requestId", header.getString("requestId"))
                .execute()
                .body());
//        String str = FileUtil.readString("D:\\乱七八糟\\obDetail.txt", "utf8");
//        JSONObject res = JSONObject.parseObject(str);

        res = res.getJSONObject("data");
        if (CollUtil.isEmpty(keys)) {
            return ResultData.success(res);
        }

        JSONObject data = new JSONObject();
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.getString(i);
            data.put(key, res.getString(key));
        }
        return ResultData.success(data);
    }

    /**
     * @author xiesm
     * @description ob列表详情
     * @date 2025/9/24
     */
    @RequestMapping("/obGameDetail")
    public ResultData obGameDetail(@RequestBody(required = false) JSONObject requestBody) {
        String url = requestBody.getString("url");
        JSONArray keys = requestBody.getJSONArray("keys");
        JSONArray hpids = requestBody.getJSONArray("hpids");
        JSONObject header = requestBody.getJSONObject("header");
        JSONObject res = JSONObject.parseObject(HttpUtil.createGet(url)
                .header("requestId", header.getString("requestId"))
                .execute()
                .body());
//        String str = FileUtil.readString("D:\\乱七八糟\\obGameDetail.txt", "utf8");
//        JSONObject res = JSONObject.parseObject(str);

        if (CollUtil.isEmpty(hpids)) {
            return ResultData.success(res.getJSONArray("data"));
        }

        JSONArray datas = new JSONArray();

        JSONArray items = res.getJSONArray("data");
        for (int i = 0; i < items.size(); i++) {
            JSONObject item = items.getJSONObject(i);
            String hpid = item.getString("hpid");
            if (!hpids.contains(hpid)) {
                continue;
            }
            if (CollUtil.isNotEmpty(keys)) {
                JSONObject data = new JSONObject();
                for (int j = 0; j < keys.size(); j++) {
                    String key = keys.getString(j);
                    data.put(key, item.get(key));
                }
                datas.add(data);
            } else {
                datas.add(item);
            }
        }
        return ResultData.success(datas);
    }
}
